package com.zyfdroid.backupftpserver.ftplets;

import org.apache.ftpserver.ftplet.FtpFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class SdcardFtpFile implements FtpFile {
    String abspath;
    File actualFile;
    File baseDir;
    public SdcardFtpFile(File basedir, String abspath) {
        this.abspath = abspath;
        this.baseDir = basedir;
        actualFile = new File(basedir,abspath);
    }

    @Override
    public String getAbsolutePath() {
        return "/sdcard"+abspath;
    }

    @Override
    public String getName() {
        return actualFile.getName();
    }

    @Override
    public boolean isHidden() {
        return false;
    }

    @Override
    public boolean isDirectory() {
        return actualFile.isDirectory();
    }

    @Override
    public boolean isFile() {
        return actualFile.isFile();
    }

    @Override
    public boolean doesExist() {
        return actualFile.exists();
    }

    @Override
    public boolean isReadable() {
        return true;
    }

    @Override
    public boolean isWritable() {
        return true;
    }

    @Override
    public boolean isRemovable() {
        return true;
    }

    @Override
    public String getOwnerName() {
        return "user";
    }

    @Override
    public String getGroupName() {
        return "user";
    }

    @Override
    public int getLinkCount() {
        return 1;
    }

    @Override
    public long getLastModified() {
        return actualFile.lastModified();
    }

    @Override
    public boolean setLastModified(long l) {
        return actualFile.setLastModified(l);
    }

    @Override
    public long getSize() {
        return actualFile.length();
    }

    @Override
    public Object getPhysicalFile() {
        return actualFile;
    }

    @Override
    public boolean mkdir() {
        return actualFile.mkdirs();
    }

    @Override
    public boolean delete() {
        if(!actualFile.exists()){return true;}
        return actualFile.delete();
    }

    @Override
    public boolean move(FtpFile ftpFile) {
        if(ftpFile instanceof SdcardFtpFile) {
            return actualFile.renameTo(((SdcardFtpFile)ftpFile).actualFile);
        }
        return false;
    }

    @Override
    public List<? extends FtpFile> listFiles() {
        List<SdcardFtpFile> files = new ArrayList<>();
        for (File s : actualFile.listFiles()) {
            files.add(new SdcardFtpFile(baseDir,s.getAbsolutePath().substring(baseDir.getAbsolutePath().length())));
        }
        return files;
    }

    @Override
    public OutputStream createOutputStream(long l) throws IOException {
        final RandomAccessFile raf = new RandomAccessFile(this.actualFile, "rw");
        raf.setLength(l);
        raf.seek(l);
        return new FileOutputStream(raf.getFD()) {
            public void close() throws IOException {
                super.close();
                raf.close();
            }
        };
    }

    @Override
    public InputStream createInputStream(final long l) throws IOException {
        final RandomAccessFile raf = new RandomAccessFile(actualFile, "r");
        raf.seek(l);
        return new FileInputStream(raf.getFD()) {
            public void close() throws IOException {
                super.close();
                raf.close();
            }
        };
    }
}
